package com.etone.eos.handler;

import com.alibaba.fastjson.JSON;
import com.etone.eos.domain.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.jafka.jeos.core.response.chain.TableRow;
import io.jafka.jeos.core.response.chain.transaction.PushedTransaction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 智慧审计 合约操作
 */
public class SmartEosHandler {

    /***
     * 账户名称
     */
    private String account;

    /***
     * 合约名称
     */
    private String code;

    /***
     * 合约表名称
     */
    private String md5TableName;


    private EosHandler handler;

    private EosConfig eosConfig;

    public SmartEosHandler(EosConfig eosConfig) {
        this.eosConfig = eosConfig;
        this.account = this.eosConfig.getAccount();
        this.code = this.eosConfig.getCode();
        this.md5TableName = this.eosConfig.getMd5TableName();
        handler = new EosHandler(eosConfig);
    }

    public CreateParamVO createParamToVO(CreateParam param) {
        CreateParamVO vo = new CreateParamVO();
        if (param != null) {
            vo.setFile_name(param.getFile_name());
            vo.setMd5(param.getMd5());
            vo.setId(param.getId());
            vo.setNm(param.getNm());
            vo.setUserId(param.getUserId());
            vo.setSha256_id(param.getSha256_id());
            vo.setAppId(param.getAppId());
        }
        return vo;
    }

    /***
     * 执行文件md5添加
     * @param param
     */
    public CreateParamVO addFileToAction(CreateParam param) {
        ActionCreateDTO createDTO = new ActionCreateDTO();
        createDTO.setAccount(account);
        createDTO.setCode(code);
        createDTO.setAction("create");
        param.setNm(account);
        createDTO.setCreateParam(param);

        CreateParamVO vo = createParamToVO(param);
        try {
            PushedTransaction pushedTransaction = handler.postAction(createDTO);
            String transactionId = pushedTransaction.getTransactionId();
            if (transactionId != null && transactionId.length() > 0) {
                //生成了交易id说明成功 返回添加的数据
                vo.setTrxId(transactionId);
                return vo;
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();

        }
        return null;
    }

    /***
     * 获取单表数据
     * @param actionId
     * @return
     */
    public MD5FileVO getTable(String actionId) {

        List<MD5FileVO> list = getTables(actionId);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 获取交易记录
     *
     * @param trxId
     * @return
     */
    public TransactionDetail getTransactionDetail(String trxId) {
//        TransactionDetail.TracesBean.ActBean.DataBeanX dataBeanX = transactionDetail.getTraces().get(0).getAct().getData();
        return handler.getTransction(trxId);
    }

    /***
     * 获取表数据
     * @param actionId 合约id
     */
    public List<MD5FileVO> getTables(String actionId) {
        Map<String, String> tableParam = new HashMap<>();
        tableParam.put("index_position", "2");
        tableParam.put("key_type", "sha256");
        tableParam.put("lower_bound", actionId);
        tableParam.put("upper_bound", actionId);
        TableParam tbparam = new TableParam();
        tbparam.setScope(account);
        tbparam.setCode(code);
        tbparam.setTable(md5TableName);
        tbparam.setParams(tableParam);

        try {
            TableRow tableRow = handler.getTableData(tbparam);
            List<Map<String, ?>> list = tableRow.getRows();
            if (list != null && list.size() > 0) {
                List<MD5FileVO> result = new ArrayList<>();
                list.stream().forEach(map -> {
                    MD5FileVO vo = JSON.parseObject(JSON.toJSONString(map), MD5FileVO.class);
                    result.add(vo);
                });
                return result;
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }


}
